package vno.sci.window
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.ColorCorrection;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import hr.binaria.asx3m.converters.basic.NullConverter;
	
	import mx.core.IFactory;
	import mx.utils.ColorUtil;
	
	import vno.fux.BasePoint;
	import vno.fux.GlobalEventScheduler;
	import vno.sci.event.MinimapEvent;
	
	public class Minimap extends Sprite
	{
		public var radius:int;
		private var _bm:Bitmap;
		private var _maskSprite:Sprite;
		/**
		 * <code>
		 * var m:Minimap;
		 * m.addEventListener(MouseEvent.Click, onClick);
		 * var onClick:Function = function (e:MouseEvent)
		 * {
		 *     //这是哪儿被点了。
		 *     trace(e.localX, e.localY);
		 * } 
		 * <code>
		 * @param radius
		 * @param bitmap
		 * @param point
		 * 
		 */		
		public function Minimap(radius:int = 150, bitmap:Bitmap = null, point:BasePoint = null)
		{		
			this.radius = radius;
					
			//遮罩层是一个半径为radius的圆
			_maskSprite = new Sprite();
			_maskSprite.graphics.lineStyle();
			_maskSprite.graphics.beginFill(0xFF0000);
			_maskSprite.graphics.drawCircle(radius,radius,radius);
			_maskSprite.graphics.endFill();
			//maskSprite.cacheAsBitmap = true;
			
			if(bitmap != null)
			{
				_bm = bitmap;
			}
			else
			{//这是一个测试图像
				var bmd:BitmapData = new BitmapData(1024,1024,false);
				for(var i:int = 0; i < 1024; i++)
				{
					for(var j:int = 0; j < 1024; j++)
					{
						bmd.setPixel(i,j,i*256+j);
					}
				}
				_bm = new Bitmap(bmd);
			}
			
			if(point!=null)
			{
				_bm.x = -point.x+radius;
				_bm.y = -point.y+radius;
			}
			//设定遮罩
			_bm.mask = _maskSprite;			
			
			var count:uint = 0;
			var redSpot:Sprite = new Sprite();
			var r:int = 4;
			
			redSpot.graphics.beginFill(0xFF0000,0.5);	
			redSpot.graphics.drawCircle(r,r,r);
			redSpot.graphics.endFill();
			redSpot.x = radius - r;
			redSpot.y = radius - r;
			redSpot.cacheAsBitmap = true;
			
			
			
			var instance:GlobalEventScheduler = GlobalEventScheduler.getInstance();
			var func:Function = function (event:Event):void
			{
				count++;
				if(count%80 < 30)
				{
					redSpot.visible = false;
				}
				else 
				{
					redSpot.visible = true;	
				}
			}
			instance.addEnterFrameListener(func);
			
			var onMouseClick:Function = function (event:MouseEvent):void
			{
				//如果在圆里且不越地图的界，放出MinimapEvent事件。
				if((event.localX - _bm.x > 0) && (event.localY - _bm.y > 0) &&
				   (event.localX - _bm.x < _bm.width) && (event.localY - _bm.y < _bm.height) &&
				   (Math.sqrt(Math.pow((event.localX-radius),2)+Math.pow((event.localY-radius),2))<radius))
				{
					dispatchEvent(new MinimapEvent(MinimapEvent.MINIMAP_CLICKED, event.localX-_bm.x, event.localY-_bm.y));
//					trace(event.localX-bm.x, event.localY-bm.y);
				}
				event.stopImmediatePropagation();
			}
			this.addChild(_bm);
			this.addChild(_maskSprite);
			
			this.addChild(redSpot);
			this.addEventListener(MouseEvent.CLICK,onMouseClick);
			
		}
		
		public function mapMove(x:int, y:int):void
		{
			_bm.x += x;
			_bm.y += y;
		}
		/**
		 * 把图像的中心点放到圆的中心点位置 
		 * @param point
		 * 
		 */		
		public function setCenter(point:BasePoint):void
		{
			_bm.x = -point.x+radius;
			_bm.y = -point.y+radius;
		}
		/**
		 * 关闭并释放资源 
		 * 
		 */		
		public function close():void
		{
			try
			{
				parent.removeChild(this);
			} 
			catch(error:Error) 
			{
				
			}
		}
	}
}